package com.yunnuo.system.shiro.service;


import com.yunnuo.system.comm.util.AsyncFactory;
import com.yunnuo.system.comm.util.AsyncManager;
import com.yunnuo.system.users.entity.User;
import com.yunnuo.system.users.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 登陆校验方法
 * Created by 魏洪福(15064000912@163.com) on  2018/5/25.
 */
@Component
public class LoginService
{
    @Autowired
    private PasswordService passwordService;

    @Autowired
    private IUserService userService;

    /**
     * 登录
     */
    public User login(String username, String password) throws Exception {
        User user =null;

        // 用户名或密码为空 错误
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
        {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username,"1","ERROR","用户名或密码为空"));
            throw new Exception("用户名或密码为空");
        }
        // 查询用户信息
          user = userService.selectUserByLoginName(username);

      if (user == null || user.getUserStatus().equals("1"))
        {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username,"1","ERROR","用户名或密码错误"));
            throw new Exception("用户名或密码错误");
        }
        passwordService.validate(user, password);
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username,"0","SUCCESS","登录成功"));
        return user;
    }


}
